package com.wx.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.constant.JedisConstant;
import com.wx.constant.MessageConstant;
import com.wx.dao.SetmealDao;
import com.wx.dao.SetmeatCheckgroupDao;
import com.wx.entity.PageResult;
import com.wx.entity.QueryPageBean;
import com.wx.entity.Result;
import com.wx.pojo.CheckGroup;
import com.wx.pojo.Setmeal;
import com.wx.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = SetmealService.class)
@Transactional // 开启事务
public class SetmealServiceImp implements SetmealService {
   @Autowired
   private SetmealDao setmealDao;
   @Autowired
   private SetmeatCheckgroupDao setmeatCheckgroupDao;
   @Autowired
   @Qualifier("jedisPool")
   private JedisPool jedisPool;
    @Override
    public PageResult findAllSetmeal(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        List<Setmeal> allSetmeal = setmealDao.findAllSetmeal(queryString);
        PageInfo<Setmeal> setmealPageInfo = new PageInfo<>(allSetmeal);
        long total = setmealPageInfo.getTotal();
        PageResult pageResult = new PageResult(total, allSetmeal);
        return pageResult;
    }

    @Override
    public PageResult findAllCheckgroup() {
        List<CheckGroup> groups = setmealDao.findAllCheckgroup();
        PageResult pageResult = new PageResult(null, groups);
        return pageResult;
    }

    /**
     * 套餐添加
     * @param setmeal
     * @param ids
     */
    @Override
    public void addSetmeal(Setmeal setmeal, Integer[] ids) {
        // 获得添加数据的主键
        setmealDao.addSetmeal(setmeal);
        Integer id = setmeal.getId();
        // 向中间表中添加数据
        for (Integer integer : ids) {
            setmeatCheckgroupDao.addSetmeatAndCheckgroup(id,integer);
        }
        // 保存完毕以后把文件名保存到redis
        jedisPool.getResource().sadd(JedisConstant.JEDIS_DATABASE,setmeal.getImg());
    }

    @Override
    public Result deleteSetemeal(Integer id) {
        // 先查询你是否有关联的数据
        Setmeal setmeal = setmealDao.findCheckgoupsBySetmealId(id);
        List<CheckGroup> checkGroups = setmeal.getCheckGroups();
        if(checkGroups.size()>0){
            // 不允许删除
            return new Result(false, "删除套餐失败");
        }else {
            // 无关联数据直接删除
            setmealDao.deleteSetmealById(id);
            return new Result(true,"删除套餐成功");
        }
    }

    @Override
    public Setmeal getSetmealById(Integer id) {
        Setmeal setmeal = setmealDao.getSetmealById(id);
        return setmeal;
    }

    @Override
    public void updateSetmeal(Setmeal setmeal, Integer[] ids) {
        Integer id = setmeal.getId();
        setmeatCheckgroupDao.deleteBySetmealiD(id);
        // 更新中间表的数据
        for (Integer integer : ids) {
            setmeatCheckgroupDao.addSetmeatAndCheckgroup(id,integer);
        }
        // 更新套餐表的数据
        setmealDao.updateSetmeal(setmeal);
    }
    public Setmeal findCheckgoupsBySetmealId(Integer id){
        Setmeal checkgoupsBySetmealId = setmealDao.findCheckgoupsBySetmealId(id);
        return checkgoupsBySetmealId;
    }

    @Override
    public List<Setmeal> findAllCheckitems() {
        return setmealDao.findAllCheckitems();
    }

}
